package com.itguigu.fruit.demo;

import java.sql.*;

public class demo02 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql:///fruitdb", "root", "123456");
        System.out.println(conn==null ? "连接失败":"链接成功");
        //3.编写SQL语句
        String sql="insert into t_fruit values(null,?,?,?,?)";
//        4.
        PreparedStatement psmt=conn.prepareStatement(sql);

//        5.具体数值
        psmt.setString(1,"苹果");
        psmt.setInt(2,2);
        psmt.setInt(3,150);
        psmt.setString(4,"ok");
        //6.返回影响行数
        int count=psmt.executeUpdate();
        System.out.println(count>0 ? "添加成功":"添加失败");
//        7.释放链接
        psmt.close();
        conn.close();
    }

}
